package com.li.controller;


import com.li.annotation.Log;
import com.li.enums.BusinessType;
import com.li.result.Result;
import com.li.service.SysMenuService;
import com.li.system.SysMenu;
import com.li.vo.AssginMenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author xiaoli
 * @since 2022-10-31
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/admin/system/sysMenu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    //根据角色分配菜单
    @ApiOperation("根据角色获取菜单")
    @GetMapping("toAssign/{roleId}")
    public Result toAssign(@PathVariable String roleId){
        List<SysMenu> list = sysMenuService.findMenuRoleId(roleId);
        return Result.ok(list);
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.assignAuth')")
    @ApiOperation("给角色分配菜单权限")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginMenuVo assginMenuVo){
        sysMenuService.doAssign(assginMenuVo);
        return Result.ok();

    }

    /*@ApiOperation("查询")
    @GetMapping("/list")
    public Result list(){
        List<SysMenu> list = sysMenuService.list(null);
        return Result.ok(list);
    }*/
    //菜单列表（树形）
    @PreAuthorize("hasAuthority('bnt.sysMenu.list')")
    @ApiOperation(value = "获取菜单")
    @GetMapping("findNodes")
    public Result findNodes() {
        List<SysMenu> list = sysMenuService.findNodes();
        return Result.ok(list);
    }

    @Log(title = "菜单管理(添加)",businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.sysMenu.add')")
    @ApiOperation("添加")
    @PostMapping("/save")
    public Result save(@RequestBody SysMenu sysMenu) {
        boolean save = sysMenuService.save(sysMenu);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @Log(title = "菜单管理(删除)",businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.sysMenu.remove')")
    @ApiOperation("删除")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable String id) {
        sysMenuService.removeMenuById(id);

        return Result.ok();

    }

    @Log(title = "菜单管理(修改)",businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('bnt.sysMenu.update')")
    @ApiOperation("修改")
    @PostMapping("/update")
    public Result update(@RequestBody SysMenu sysMenu) {
        boolean update = sysMenuService.updateById(sysMenu);
        if (update) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @PreAuthorize("hasAuthority('bnt.sysMenu.list')")
    @ApiOperation("根据Id查询")
    @GetMapping("/findNode/{id}")
    public Result findNode(@PathVariable String id) {
        SysMenu sysMenu = sysMenuService.getById(id);
        return Result.ok(sysMenu);
    }

    @ApiOperation("更改菜单状态")
    @GetMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable String id,@PathVariable Integer status){
        sysMenuService.updateStatus(id,status);
        return Result.ok();
    }

}

